package com.ws.universal.tools.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import androidx.annotation.CallSuper;
import androidx.appcompat.app.AppCompatActivity;

import com.gyf.barlibrary.ImmersionBar;
import com.ws.universal.tools.R;
import com.ws.universal.tools.permission.SettingService;
import com.ws.universal.tools.utils.LanguageUtils;
import com.ws.universal.tools.utils.LocationUtils;


public abstract class BaseActivity extends AppCompatActivity {
    public SettingService mSettingService;
    protected BaseProgressDialog mProgressDialog;
    private Context context;
    private Activity activity;
    private Location location;
    //private final BehaviorSubject<ActivityEvent> lifecycleSubject = BehaviorSubject.create();

   /* @NonNull
    @CheckResult
    public final Observable<ActivityEvent> lifecycle() {
        return this.lifecycleSubject.hide();
    }

    @NonNull
    @CheckResult
    public final <T> LifecycleTransformer<T> bindUntilEvent(@NonNull ActivityEvent event) {
        return RxLifecycle.bindUntilEvent(this.lifecycleSubject, event);
    }

    @NonNull
    @CheckResult
    public final <T> LifecycleTransformer<T> bindToLifecycle() {
        return RxLifecycleAndroid.bindActivity(this.lifecycleSubject);
    }*/

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //this.lifecycleSubject.onNext(ActivityEvent.CREATE);
//        //沉浸式
//        ImmersionBar.with(this)
//                .statusBarDarkFont(true)
//                .statusBarColor(R.color.white)
//                .fitsSystemWindows(true)  //使用该属性必须指定状态栏的颜色，不然状态栏透明，很难看
//                .init();
        ImmersionBar.with(this).statusBarDarkFont(false).init();
        context = this;
        activity = this;
        mProgressDialog = new BaseProgressDialog(context);
        initCreate(savedInstanceState);
        initView();
    }

    public Activity getActivity() {
        return activity;
    }



    public Location getLocation() {
        return LocationUtils.getInstance().getLocation();
    }


    protected abstract void initCreate(Bundle savedInstanceState);

    protected abstract void initView();

    protected abstract void FinishDesTroy();

    public Context getContext() {
        return context;
    }

    public void showProgressDialog(boolean cancelable) {
        showProgressDialog(cancelable, getResources().getString(R.string.loading));
    }

    public void showProgressDialog(boolean cancelable, String msg) {
        showProgressDialog(null, cancelable, msg);
    }

    public void showProgressDialog(BaseProgressDialog.OnCancelListener cancelListener, boolean cancelable, String msg) {
        if (mProgressDialog != null && mProgressDialog.isShowing()) {
            return;
        }
        mProgressDialog.setMessage(msg);

        if (cancelListener != null) {
            mProgressDialog.setOnCancelListener(cancelListener);
        }
        mProgressDialog.setCancelable(cancelable);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mProgressDialog.show();
            }
        });

    }

    public void stopProgressDialog() {
        if (mProgressDialog != null && mProgressDialog.isShowing()) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mProgressDialog.stop();
                }
            });

        }
    }

    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(newBase);
        LanguageUtils.attachBaseContext(newBase);
    }

    @CallSuper
    protected void onStart() {
        super.onStart();
        // this.lifecycleSubject.onNext(ActivityEvent.START);
    }

    @CallSuper
    protected void onResume() {
        super.onResume();
        // this.lifecycleSubject.onNext(ActivityEvent.RESUME);
    }

    @CallSuper
    protected void onPause() {
        // this.lifecycleSubject.onNext(ActivityEvent.PAUSE);
        super.onPause();
    }

    @CallSuper
    protected void onStop() {
        // this.lifecycleSubject.onNext(ActivityEvent.STOP);
        super.onStop();
    }


    @Override
    protected void onDestroy() {
        // this.lifecycleSubject.onNext(ActivityEvent.DESTROY);
        FinishDesTroy();
        super.onDestroy();
    }

    /**
     * 不带参数的跳转
     *
     * @param clazz 跳转到的目标类
     */
    protected void pageJump(final Class<?> clazz) {
        Intent intent = new Intent(getContext(), clazz);
        startActivity(intent);
    }

    /**
     * 带参数的跳转
     *
     * @param clazz  跳转到的目标类
     * @param bundle 参数
     */
    protected void pageJump(final Class<?> clazz, final Bundle bundle) {
        Intent intent = new Intent(getContext(), clazz);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    /**
     * 跳转然后退出当前页
     *
     * @param clazz 跳转到的目标类
     */
    protected void pageJumpKill(final Class<?> clazz) {
        Intent intent = new Intent(getContext(), clazz);
        startActivity(intent);
        finish();
    }

    /**
     * 带参数跳转然后退出当前页
     *
     * @param clazz  跳转到的目标类
     * @param bundle 参数
     */
    protected void pageJumpKill(final Class<?> clazz, final Bundle bundle) {
        Intent intent = new Intent(getContext(), clazz);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
        finish();
    }

    /***
     * 隐藏键盘
     * @param mEtPassword
     */
    public void hideSoftInput(View mEtPassword) {
        mEtPassword.clearFocus();
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(mEtPassword.getWindowToken(), 0); //强制隐藏键盘
    }

    /***
     * 显示键盘
     * @param editText
     */
    public void showSoftInput(EditText editText) {
        editText.setFocusableInTouchMode(true);
        editText.requestFocus();
        editText.setSelection(editText.getText().length());
        InputMethodManager imm = (InputMethodManager) editText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            imm.showSoftInput(editText, 0);
        }
    }


    public void hideBottomUi() {
        View decorView = getWindow().getDecorView();
        int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
        decorView.setSystemUiVisibility(uiOptions);
    }

}
